package org.vectrics.cms;

import javax.servlet.jsp.JspException;

import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;
import org.vectrics.user.UserPrivilegeType;

public class ShowContentAction extends VectricsStrutsAction {

	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		String forward = "success";
		log.info("request class: " + actionData.getRequest().getClass().getName());
		
		String section = actionData.getRequest().getParameter("section");
		String code = actionData.getRequest().getParameter("code");
		ContentHelper contentHelper = new ContentHelper();
		
		String content = contentHelper.getContent(ThreadProperties.getDomain(), section, code);
		ContentEditForm form = new ContentEditForm();
		form.setCode(code);
		form.setSection(section);
		form.setContent(content);
		
		actionData.setRequestForm(form);
		
		Person authPerson = actionData.getAuthPerson();
		CmsSection cmsSection = CoreServiceLocator.getSystemService().findCmsSection(actionData.getAuthDomain(), section);
		UserPrivilegeType requiredPrivilege = cmsSection.getUserPrivilegeType();
		boolean canEditCms = authPerson.hasDomainOrGlobalPrivilege(requiredPrivilege, actionData.getAuthDomain());
		if (canEditCms == false) {
			forward = "not.authorized";
		}
		
		
		return forward;
	}

}
